package com.sprouting.personal.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.personal.dao.ShoppingCartDao;
import com.sprouting.personal.dao.UploadFileDao;
import com.sprouting.personal.model.entity.ShoppingCart;
import com.sprouting.personal.model.entity.UploadFile;
import com.sprouting.personal.model.vo.ShoppingCartVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车
 *
 * @author ：LX（长沙麓谷）
 * @date 创建时间： 2022/9/13 0:46
 */
@Slf4j
@Service
public class ShoppingCartService extends ServiceImpl<ShoppingCartDao, ShoppingCart> {

    @Resource
    private ShoppingCartDao shoppingCartDao;
    @Resource
    private UploadFileDao uploadFileDao;

    /**
     * 根据购物名称查询
     * @param id 购物id，传了该ID，则会忽略查询该数据
     * @param userId 当前用户ID
     * @param name 物品名称
     * @return
     */
    public ShoppingCart getShoppingCartRepeatQuery(String id, String userId, String name) {
        return shoppingCartDao.getShoppingCartRepeatQuery(id, userId, name);
    }

    /**
     * 保存购物信息和图片信息
     * @param shoppingCart 购物信息
     * @param uploadFileList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveShoppingCart(ShoppingCart shoppingCart, List<UploadFile> uploadFileList) {
        shoppingCartDao.insert(shoppingCart);
        if (CollUtil.isNotEmpty(uploadFileList)){
            // todo 优化点
            for (UploadFile uploadFile : uploadFileList){
                uploadFileDao.insert(uploadFile);
            }
        }
    }

    /**
     * 更新购物信息和图片信息
     * @param shoppingCart 购物信息
     * @param uploadFileList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void updShoppingCart(ShoppingCart shoppingCart, List<UploadFile> uploadFileList) {
        shoppingCartDao.updateById(shoppingCart);
        if (CollUtil.isNotEmpty(uploadFileList)){
            // todo 优化点
            for (UploadFile uploadFile : uploadFileList){
                uploadFileDao.insert(uploadFile);
            }
        }
    }

    /**
     * 删除购物信息
     * @param shoppingCart 购物信息
     * @param uploadFileList 图片信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void delShoppingCart(ShoppingCart shoppingCart, List<UploadFile> uploadFileList) {
        shoppingCartDao.deleteById(shoppingCart.getId());
        if (CollUtil.isNotEmpty(uploadFileList)){
            List<String> idList = uploadFileList.stream().map(UploadFile::getId).collect(Collectors.toList());
            uploadFileDao.deleteBatchIds(idList);
        }
    }

    /**
     * 根据主键查询物品信息
     * @param id 主键
     * @return
     */
    public ShoppingCart getShoppingCartById(String id) {
        return shoppingCartDao.getShoppingCartById(id);
    }

    /**
     * 分页查询购物信息
     * @param page 分页
     * @param shoppingCart 购物信息
     * @return
     */
    public Page<ShoppingCartVo> getShoppingCartListPage(Page page, ShoppingCart shoppingCart) {
        return shoppingCartDao.getShoppingCartListPage(page, shoppingCart);
    }

    /**
     * 根据当前用户ID查询购物车总数
     * @param userId 当前用户ID
     * @return
     */
    public Long getShoppingCartTotal(String userId) {
        return shoppingCartDao.getShoppingCartTotal(userId);
    }
}
